<template>
  <div
    class="vinput-box"
    :class="itemdata.lCanInput === '0' ? 'disable-color' : ''"
  >
    <template v-if="ishead">
      <template v-if="itemdata.lCanOutput === '0'">
        <input @focus="iptFoucus" v-model="itemdata.value" style="display: none" />
      </template>
      <template
        v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'"
      >
        <input @focus="iptFoucus"
          type="hidden"
          :id="itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          :id="itemdata.sField + '_enum'"
          required
          placeholder="系统自动填充"
          readonly
          v-model="itemdata.value"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input @focus="iptFoucus"
          type="hidden"
          :id="itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          v-if="itemdata.sUITypeExp == 'normal'"
          placeholder="请输入"
          :id="itemdata.sField + '_enum'"
          required
          readonly
          v-model="itemdata.value"
          @click="openEnum(itemdata)"
          :error-message="itemdata.errormsg"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          v-else-if="itemdata.sUITypeExp == 'multi'"
          placeholder="请输入"
          :id="itemdata.sField + '_enum'"
          required
          readonly
          v-model="itemdata.value"
          @click="openEnumMulti(itemdata)"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input @focus="iptFoucus"
          type="hidden"
          :id="itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          :id="itemdata.sField + '_enum'"
          placeholder="系统自动填充"
          readonly
          v-model="itemdata.value"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else>
        <input @focus="iptFoucus"
          type="hidden"
          :id="itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          v-if="itemdata.sUITypeExp == 'normal'"
          placeholder="请输入"
          :id="itemdata.sField + '_enum'"
          v-model="itemdata.value"
          readonly
          @click="openEnum(itemdata)"
          :error-message="itemdata.errormsg"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          v-else-if="itemdata.sUITypeExp == 'multi'"
          placeholder="请输入"
          :id="itemdata.sField + '_enum'"
          v-model="itemdata.value"
          readonly
          @click="openEnumMulti(itemdata)"
          :error-message="itemdata.errormsg"
        />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'normal'">
      <template v-if="itemdata.lCanOutput === '0'">
        <input @focus="iptFoucus" v-model="itemdata.value" style="display: none" />
      </template>
      <template
        v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'"
      >
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          :id="index + '_' + itemdata.sField + '_enum'"
          required
          placeholder="系统自动填充"
          readonly
          v-model="itemdata.value"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_enum'"
          required
          readonly
          v-model="itemdata.value"
          @click="openEnum(itemdata)"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          :id="index + '_' + itemdata.sField + '_enum'"
          placeholder="系统自动填充"
          readonly
          v-model="itemdata.value"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else>
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_enum'"
          readonly
          v-model="itemdata.value"
          @click="openEnum(itemdata)"
          :error-message="itemdata.errormsg"
        />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'multi'">
      <template v-if="itemdata.lCanOutput === '0'">
        <input @focus="iptFoucus" v-model="itemdata.value" style="display: none" />
      </template>
      <template
        v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'"
      >
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          :id="index + '_' + itemdata.sField + '_enum'"
          required
          placeholder="系统自动填充"
          readonly
          v-model="itemdata.value"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_enum'"
          required
          readonly
          v-model="itemdata.value"
          @click="openEnumMulti(itemdata)"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          :id="index + '_' + itemdata.sField + '_enum'"
          placeholder="系统自动填充"
          readonly
          v-model="itemdata.value"
          :error-message="itemdata.errormsg"
        />
      </template>
      <template v-else>
        <input @focus="iptFoucus"
          type="hidden"
          :id="index + '_' + itemdata.sField"
          v-model="itemdata.valuehidden"
        />
        <input @focus="iptFoucus"
          class="vinput"
          size="mini"
          placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_enum'"
          readonly
          v-model="itemdata.value"
          @click="openEnumMulti(itemdata)"
          :error-message="itemdata.errormsg"
        />
      </template>
    </template>
    <van-action-sheet
      :get-container="getContainer"
      v-model="enumshow"
      :actions="actions"
      @select="selectedEnum"
    />
    <van-popup
      :get-container="getContainer"
      v-model="enumMultishow"
      round
      position="bottom"
    >
      <van-checkbox-group v-model="selecteddata" ref="checkboxGroup">
        <van-cell
          v-for="(item, index) in enumMulti"
          :key="item.id"
          :title="item.name"
          @click="toggle(index)"
        >
          <van-checkbox :name="item" ref="checkboxes" slot="right-icon" />
        </van-cell>
      </van-checkbox-group>
      <div
        style="
          text-align: right;
          padding-right: 10vw;
          padding-top: 2vh;
          padding-bottom: 2vh;
        "
      >
        <van-button type="info" size="small" @click="selectedEnumMulti"
          >确认</van-button
        >
      </div>
    </van-popup>
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      enumshow: false,
      enumMultishow: false,
      enumMulti: [],
      selecteddata: [],
      clickmodel: {},
      actions: [],
      x: [],
    };
  },

  props: {
    jsondata: {
      type: Object,
      required: false,
    },
    idobject: {
      type: String,
      required: false,
    },
    itemdata: {
      type: Object,
      required: false,
    },
    ishead: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      required: false,
    },
  },
  watch: {
    "itemdata.value": {
      handler(newval) {
        this.$emit("handleFiledLink", {
          sField: this.itemdata.sField,
          value: newval,
          ishead: this.ishead,
          index: this.index,
        });
      },
    },
  },
  methods: {
    iptFoucus() {
      document.activeElement.blur();
    },
    getContainer() {
      return document.querySelector("body");
    },
    selectedEnumMulti() {
      let enumid = "";
      let enumval = "";
      this.clickmodel.valuehidden = "";
      this.clickmodel.value = "";
      for (let i = 0; i < this.selecteddata.length; i++) {
        enumid += this.selecteddata[i].id + ";";
        enumval += this.selecteddata[i].name + ";";
      }
      this.clickmodel.valuehidden = enumid;
      this.clickmodel.value = enumval;
      this.clickmodel.errormsg = "";
      this.enumMultishow = false;
    },
    selectedEnum(item) {
      this.clickmodel.valuehidden = item.id;
      this.clickmodel.value = item.name;
      this.clickmodel.errormsg = "";
      this.enumshow = false;
      this.boolUpdate(this.clickmodel);
    },
    openEnum(datamodel) {
      this.actions = [];
      this.clickmodel = datamodel;
      let enumid = datamodel.allenumid;
      let arrEnumid = enumid.split(";");
      let enumval = datamodel.allenumval;
      let arrEnumval = enumval.split(";");
      for (let i = 0; i < arrEnumid.length; i++) {
        this.actions.push({ id: arrEnumid[i], name: arrEnumval[i] });
      }
      this.enumshow = true;
    },
    openEnumMulti(datamodel) {
      this.enumMulti = [];
      this.selecteddata = [];
      this.clickmodel = datamodel;
      let enumid = datamodel.allenumid;
      let arrEnumid = enumid.split(";");
      let enumval = datamodel.allenumval;
      let arrEnumval = enumval.split(";");
      for (let i = 0; i < arrEnumid.length; i++) {
        if (arrEnumid[i] === "") {
          continue;
        }
        this.enumMulti.push({ id: arrEnumid[i], name: arrEnumval[i] });
      }
      this.enumMultishow = true;
    },
    boolUpdate(clickmodel) {
      //console.log('boolUpdate22')
      let data = {};
      if (clickmodel.idObject === "sorder" && clickmodel.sField === "sfdj") {
        let sfdjval = clickmodel.value === "是" ? "带价格 " : "";
        let fcval = "";
        data[this.idobject + "_id"] = this.jsondata[this.idobject + "_id"];
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue;
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === "fc") {
              fcval = itemgroup[itemindex].value;
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === "snote") {
              itemgroup[itemindex].value = sfdjval + fcval;
            }
          }
        }
      }
      if (clickmodel.idObject === "sorder" && clickmodel.sField === "fc") {
        let sfdjval = "";
        let fcval = clickmodel.value;
        data[this.idobject + "_id"] = this.jsondata[this.idobject + "_id"];
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue;
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === "sfdj") {
              sfdjval = itemgroup[itemindex].value === "是" ? "带价格 " : "";
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === "snote") {
              itemgroup[itemindex].value = sfdjval + fcval;
            }
          }
        }
      }
    },
  },
};
</script>
